extends CanvasLayer

onready var continueButton = $MarginContainer/PanelContainer/MarginContainer/VBoxContainer/ContinueButton
onready var optionButton = $MarginContainer/PanelContainer/MarginContainer/VBoxContainer/OptionButton
onready var quitButton = $MarginContainer/PanelContainer/MarginContainer/VBoxContainer/QuitButton

var optionsMenuScene = preload("res://scenes/UI/OptionMenu.tscn")

func _ready():
	continueButton.connect("pressed", self, "on_continue_pressed")
	optionButton.connect("pressed", self, "on_options_pressed")
	quitButton.connect("pressed", self, "on_quit_pressed")
	get_tree().paused = true	# 停止已知的物理过程
	# 将 PauseMenu 检查器中的 Pause Mode 设置为 Process 可以使根节点的子节点不受暂停树的影响

func _unhandled_input(event):
	if (event.is_action_pressed("pause")):
		unpause()
		get_tree().set_input_as_handled()	# 防止重复处理这一事件

func unpause():
	queue_free()
	get_tree().paused = false

func on_continue_pressed():
	yield(get_tree().create_timer(.1), "timeout")	
	unpause()
	
func on_options_pressed():
	var optionMenuInstance = optionsMenuScene.instance()
	add_child(optionMenuInstance)
	optionMenuInstance.connect("back_pressed", self, "on_option_back_pressed")
	$MarginContainer.visible = false
	
func on_option_back_pressed():
	$OptionMenu.queue_free()
	$MarginContainer.visible = true
	
	
func on_quit_pressed():
	yield(get_tree().create_timer(.1), "timeout")	
	$"/root/SceenTransitionManager".transition_to_menu()
	unpause()
